﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Tarea5
{
    class ModelTTTHH : ModelBase
    {
        /// <summary>
        /// A the stack of visited states.
        /// </summary>
        private Stack<StateTTT> statesStack;

        private StateTTT state;

        override public void Initialize(int sleepInterval, int size, bool nextTurnIsCrossPlayerS)
        {
            base.Initialize(sleepInterval, size, nextTurnIsCrossPlayerS);
            statesStack = new Stack<StateTTT>();
        }

        override protected void ModelStart()
        {
            ///VOID METHOD, NO ALGORITHM IN INTERACTIVE MODE!
        }

        override public int SetPosition(int row, int col) 
        {
            if (state == null || state.Index == 0)
            {
                state = new StateTTT(this.size, row, col);
            }
            else
            {
                StateTTT tmp = state.GetNextState(row, col);

                if (tmp == null) return -1;

                statesStack.Push(state);
                state = tmp;
            }

            NotifyAll(this.state, "", "");

            return this.state.Index;
        }

        override public int UndoLastMove()
        {

            int index = 0;

            try
            {
                if (statesStack.Count == 0)
                {
                    state = new StateTTT(this.size);
                    NotifyAll(state, "", "");
                }
                else
                {
                    index = state.Index;
                    state = statesStack.Pop();
                    NotifyAll(this.state, "", "");
                }
                return index;
            }
            catch(InvalidOperationException e) { }

            return -1;
        }

        override public void Start() { }

        override public void Pause() { }

        override public void Stop() { }
    }
}
